let exchangeDao = module.exports;
let mysqlPool = require('../dao/MySQLPool');

exchangeDao.getRecordByPage = async function (uid, page, count) {
    page--;
    if (page < 0){
        page = 0;
    }

    let sql = 'select * from exchange where status = 1 and uid = ?  order by id desc limit ?, ?';
    let args = [uid, (page * count), count];
    let res = await mysqlPool.query(sql, args);
    if (res){
        return res;
    }

    return null;
};

/**
 * 
 */
exchangeDao.updateStatusWithTx = async function(conn, record){
    let sql = 'update exchange set status = ?, operator = ? ';
    let args=[record.status, record.operator];

    if (record.remark){
        args.push(record.remark);
        sql += ' , remark = ? ';
    }

    sql += ' where flow_no = ? ';
    args.push(record.flowNo);

    let res = await conn.query(sql, args);
    return res.affectedRows;
};

exchangeDao.getRecordByFlowNoAndStatus = async function(flowNo, status){
    let sql = 'select * from exchange where flow_no = ? and status = ? limit 1';
    let args = [flowNo, status];
    let res = await mysqlPool.query(sql, args);
    if (res){
        return res[0];
    }

    return null;
};

exchangeDao.getRecordByFlowNo = async function(flowNo){
    let sql = 'select * from exchange where flow_no = ? limit 1';
    let args = [flowNo];
    let res = await mysqlPool.query(sql, args);
    if (res){
        return res[0];
    }

    return null;
};

/**
 * 查询提现记录
 * @param cond
 *      cond.uid
 *      cond.flowNo
 *      cond.status
 *      cond.type
 *      cond.startTime
 *      cond.endTime
 *      cond.page
 *      cond.count
 * @returns {Promise<*>}
 */
exchangeDao.queryPageByCond = async function(cond){
    let sql = 'select e.*, u.name from ' +
        ' exchange e, users u ' +
        ' where 1 = 1 and e.uid = u.uid ';

    let args = [];
    if (cond.uid){
        args.push(cond.uid);
        sql += ' and e.uid = ?';
    }

    if (cond.flowNo){
        args.push(cond.flowNo);
        sql += ' and e.flow_no = ?';
    }

    if (cond.status){
        args.push(cond.status);
        sql += ' and e.status = ?';
    }

    if (cond.type){
        args.push(cond.type);
        sql += ' and e.type = ?';
    }

    if (cond.startTime){
        args.push(cond.createTime);
        sql += ' and e.create_time >= ?';
    }

    if (cond.endTime){
        args.push(cond.endTime);
        sql += ' and e.create_time <= ?';
    }

    sql += ' order by e.create_time desc ';

    if (!cond.page) cond.page = 1;
    if (!cond.count) cond.count = 10;

    args.push((cond.page - 1) * cond.count);
    args.push(cond.count);
    sql += ' limit ?, ?';

    let res = await mysqlPool.query(sql, args);
    if (res){
        return res;
    }

    return null;
};

exchangeDao.queryPageCntByCond = async function(cond){
    let sql = 'select count(1) cnt from ' +
        ' exchange e, users u ' +
        ' where 1 = 1 and e.uid = u.uid ';

    let args = [];
    if (cond.uid){
        args.push(cond.uid);
        sql += ' and e.uid = ?';
    }

    if (cond.flowNo){
        args.push(cond.flowNo);
        sql += ' and e.flow_no = ?';
    }

    if (cond.status){
        args.push(cond.status);
        sql += ' and e.status = ?';
    }

    if (cond.type){
        args.push(cond.type);
        sql += ' and e.type = ?';
    }

    if (cond.startTime){
        args.push(cond.createTime);
        sql += ' and e.create_time >= ?';
    }

    if (cond.endTime){
        args.push(cond.endTime);
        sql += ' and e.create_time <= ?';
    }

    let res = await mysqlPool.query(sql, args);
    if (res){
        return res[0].cnt;
    }

    return 0;
};

exchangeDao.getRecordCount = async function (uid) {
    let sql = 'select count(1) cnt from exchange where status = 1 and uid = ?';
    let args = [uid];
    let res = await mysqlPool.query(sql, args);
    if (res){
        return res[0].cnt;
    }
    return 0;
};

exchangeDao.save = async function (record) {
    let sql = 'insert into exchange (uid, flow_no, ex_amt, type, ex_no, real_ex_amt, create_time, ex_fee, ex_fee_rate)' +
        '                    values (?,   ?,       ?,      ?,    ?,     ?,            now(),      ?,      ?)';
    let args = [record.uid, record.flowNo, record.exAmt, record.type, record.exNo, record.realExAmt, record.exFee, record.exFeeRate];
    let res = await mysqlPool.query(sql, args);
    if (res){
        return res.insertId;
    }

    return 0;
};

exchangeDao.saveWithTx = async function (conn, record) {
    let sql = 'insert into exchange (uid, flow_no, ex_amt, type, ex_no, real_ex_amt, create_time, ex_fee, ex_fee_rate)' +
        '                    values (?,   ?,       ?,      ?,    ?,     ?,            now(),      ?,      ?)';
    let args = [record.uid, record.flowNo, record.exAmt, record.type, record.exNo, record.realExAmt, record.exFee, record.exFeeRate];
    let res = await conn.query(sql, args);
    if (res){
        return res.insertId;
    }

    return 0;
};